package com.suning.sawp.in.controller.task;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.reflect.TypeToken;
import com.suning.sawp.constants.ConfigConstants;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.dto.common.EmployeeInfo;
import com.suning.sawp.dto.common.QueryResult;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.precharge.CommonRtn;
import com.suning.sawp.dto.task.AddTaskRtn;
import com.suning.sawp.dto.task.CategoryBean;
import com.suning.sawp.dto.task.MainPushTaskRtn;
import com.suning.sawp.dto.task.ModelUrl;
import com.suning.sawp.dto.task.QueryCommInfo;
import com.suning.sawp.dto.task.QueryRegionAssign;
import com.suning.sawp.dto.task.RegionTaskPercentBean;
import com.suning.sawp.dto.task.TaskHqInfoRtn;
import com.suning.sawp.in.controller.DjBaseController;
import com.suning.sawp.in.controller.MyGsonView;
import com.suning.sawp.in.filter.RequestUserHolder;
import com.suning.sawp.intf.task.MainPushTaskManageService;
import com.suning.sawp.service.util.ConfigUtil;
import com.suning.sawp.service.util.ExcelUtils;
import com.suning.sawp.service.util.GsonUtils;

@RequestMapping("/adminPage/task/hq")
@Controller
public class MainPushTaskManageController extends DjBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);

    @Resource
    MainPushTaskManageService   mainPushTaskManageService;

    /**
     * 
     * 功能描述:查询主推任务列表 <br>
     * 〈功能详细描述〉
     * 
     * @param taskTopic
     * @param startTime
     * @param endTime
     * @param pageNo
     * @param pageSize
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryTask.do")
    public MyGsonView queryMainPushTask(String taskTopic, String startTime, String endTime,
            Integer pageNo, Integer pageSize) {
        LOGGER.debug("queryTask input taskTopic:" + taskTopic + ",startTime:" + startTime
                + ",endTime:" + endTime);
        ReturnMsg<QueryResult<MainPushTaskRtn>> ret = new ReturnMsg<QueryResult<MainPushTaskRtn>>();
        try {
            ret = mainPushTaskManageService.queryMainPushTask(taskTopic, startTime, endTime,
                    pageNo, pageSize);
        } catch (Exception e) {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E999,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            LOGGER.error("query task faied", e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述: 新增主推任务<br>
     * 〈功能详细描述〉
     * 
     * @param taskTopic
     * @param startTime
     * @param endTime
     * @param categoryName
     * @param categoryCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/addTask.do")
    public MyGsonView addTask(String taskTopic, String startTime, String endTime,
            String categoryName, String categoryCode) {
        LOGGER.debug("addTask input taskTopic:" + taskTopic + ",startTime:" + startTime
                + ",endTime:" + endTime + ",categoryName:" + categoryName + ",categoryCode:"
                + categoryCode);
        ReturnMsg<AddTaskRtn> ret = new ReturnMsg<AddTaskRtn>();
        EmployeeInfo requestUser = RequestUserHolder.getRequestUser();
        try {
            ret = mainPushTaskManageService.addMainPushTask(taskTopic, startTime, endTime,
                    categoryCode, categoryName,requestUser.getStaffId());
        } catch (Exception e) {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E999,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            LOGGER.error("add task faied", e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述: 查询大区目标占比<br>
     * 〈功能详细描述〉
     * 
     * @param taskNo
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryPercent.do")
    public MyGsonView queryRegionPercent(Integer taskNo) {
        ReturnMsg<List<RegionTaskPercentBean>> ret = new ReturnMsg<List<RegionTaskPercentBean>>();
        try {
            ret = mainPushTaskManageService.queryRegionPercent(taskNo);
        } catch (Exception e) {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E999,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            LOGGER.error("queryPercent faied", e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述: 查询主推商品列表<br>
     * 〈功能详细描述〉
     * 
     * @param taskNo
     * @param pageNo
     * @param pageSize
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryCommList.do")
    public MyGsonView queryCommList(Integer taskNo, Integer pageNo, Integer pageSize) {
        ReturnMsg<QueryResult<QueryCommInfo>> ret = new ReturnMsg<QueryResult<QueryCommInfo>>();
        try {
            ret = mainPushTaskManageService.queryCommInfoHq(taskNo, pageNo, pageSize);
        } catch (Exception e) {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E999,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            LOGGER.error("queryCommList faied", e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述: 查询大区商品分配详情<br>
     * 〈功能详细描述〉
     * 
     * @param commClassifyCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryAssign.do")
    public MyGsonView queryAssignComm(Integer commClassifyCode, Integer taskNo) {
        ReturnMsg<List<QueryRegionAssign>> ret = new ReturnMsg<List<QueryRegionAssign>>();
        try {
            ret = mainPushTaskManageService.queryRegionAssign(commClassifyCode, taskNo);
        } catch (Exception e) {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E999,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            LOGGER.error("queryCommList faied", e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述: 修改主推商品台数<br>
     * 〈功能详细描述〉
     * 
     * @param commList
     * @param taskNo
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/updateCommNum.do")
    public MyGsonView updateCommNum(String commList, Integer taskNo) {
        LOGGER.debug("updateCommNum input commList:" + commList + ",taskNo:" + taskNo);
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        try {
            List<String> list = GsonUtils.fromJson(commList, new TypeToken<List<String>>() {
            });
            ret = mainPushTaskManageService.updateCommHqDestNum(list, taskNo);
        } catch (Exception e) {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E999,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            LOGGER.error("updateCommNum faied", e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述: 修改大区商品台数<br>
     * 〈功能详细描述〉
     * 
     * @param commList
     * @param commClassifyCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/updateRegionComm.do")
    public MyGsonView updateRegionCommNum(String commList, Integer commClassifyCode,
            Integer destNumHq) {
        LOGGER.debug("updateRegionCommNum input commList:" + commList + ",commClassifyCode:"
                + commClassifyCode);
        EmployeeInfo requestUser = RequestUserHolder.getRequestUser();
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        try {
            List<String> list = GsonUtils.fromJson(commList, new TypeToken<List<String>>() {
            });
            ret = mainPushTaskManageService.updateCommRegionDestNum(list, commClassifyCode,
                    destNumHq,requestUser.getStaffId());
        } catch (Exception e) {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E999,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            LOGGER.error("updateRegionCommNum faied", e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述: 查询任务内容<br>
     * 〈功能详细描述〉
     * 
     * @param taskNo
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryTaskInfo.do")
    public MyGsonView queryTaskInfo(Integer taskNo) {
        ReturnMsg<TaskHqInfoRtn> ret = new ReturnMsg<TaskHqInfoRtn>();
        try {
            ret = mainPushTaskManageService.queryTaskHqInfo(taskNo);
        } catch (Exception e) {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E999,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            LOGGER.error("updateRegionCommNum faied", e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述:修改目标占比 <br>
     * 〈功能详细描述〉
     * 
     * @param percentList
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/updateRegionPercent.do")
    public MyGsonView updateRegionPercent(String percentList, Integer taskNo) {
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        EmployeeInfo requestUser = RequestUserHolder.getRequestUser();
        try {
            List<String> list = GsonUtils.fromJson(percentList, new TypeToken<List<String>>() {
            });
            ret = mainPushTaskManageService.updatePercent(list, taskNo,requestUser.getStaffId());
        } catch (Exception e) {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E999,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            LOGGER.error("updateRegionPercent faied", e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述: 删除主推任务<br>
     * 〈功能详细描述〉
     * 
     * @param taskNo
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/deleteTask.do")
    public MyGsonView deleteTask(Integer taskNo,String taskTopic) {
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        EmployeeInfo requestUser = RequestUserHolder.getRequestUser();
        try {
            ret = mainPushTaskManageService.deleteTaskByTaskNo(taskNo,taskTopic,requestUser.getStaffId());
        } catch (Exception e) {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E999,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            LOGGER.error("deleteTask faied", e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述: 删除主推商品<br>
     * 〈功能详细描述〉
     * 
     * @param commClassifyCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/deleteComm.do")
    public MyGsonView deleteComm(Integer commClassifyCode,String commName) {
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        EmployeeInfo requestUser = RequestUserHolder.getRequestUser();
        try {
            ret = mainPushTaskManageService.deleteCommBycommCode(commClassifyCode,commName,requestUser.getStaffId());
        } catch (Exception e) {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E999,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            LOGGER.error("deleteComm faied", e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     * 
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryCategory.do")
    public MyGsonView queryCategory() {
        ReturnMsg<List<CategoryBean>> ret = new ReturnMsg<List<CategoryBean>>();
        try {
            ret = mainPushTaskManageService.queryCategoryList();
        } catch (Exception e) {
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E999,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
            LOGGER.error("queryCategory faied", e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述:导入大区目标占比 〈功能详细描述〉
     * 
     * @param regionCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/importPercentFile.do")
    public MyGsonView importRegionPercent(
            @RequestParam(value = "file", required = true) MultipartFile file, Integer taskNo) {
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        // 创建对Excel工作簿文件的引用
        Workbook wookbook = null;
        try {
            wookbook = ExcelUtils.createWb(file);
            // 默认取第一个sheet
            Sheet sheet = wookbook.getSheetAt(0);
            ret = mainPushTaskManageService.importRegionPercent(sheet, taskNo);
        } catch (IOException e) {
            LOGGER.error("导入目标占比解析excel出错", e);
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E007,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
        } catch (Exception e) {
            LOGGER.error("导入目标占比失败", e);
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E007,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述:导入总部主推商品 〈功能详细描述〉
     * 
     * @param regionCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/importCommFile.do")
    public MyGsonView importCommHq(
            @RequestParam(value = "file", required = true) MultipartFile file, Integer taskNo) {
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        // 创建对Excel工作簿文件的引用
        Workbook wookbook = null;
        EmployeeInfo requestUser = RequestUserHolder.getRequestUser();
        try {
            wookbook = ExcelUtils.createWb(file);
            // 默认取第一个sheet
            Sheet sheet = wookbook.getSheetAt(0);
            ret = mainPushTaskManageService.importRegionCommodity(sheet, taskNo,requestUser.getStaffId());
        } catch (IOException e) {
            LOGGER.error("导入主推解析excel出错", e);
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E007,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
        } catch (Exception e) {
            LOGGER.error("导入主推商品失败", e);
            ret.setError(ErrorCodeConstants.MAIN_PUSH_ERR_E007,
                    ErrorCodeConstants.MAIN_PUSH_ERR_MAP);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述: 查询下载地址<br>
     * 〈功能详细描述〉
     * 
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryModelAddress.do")
    public MyGsonView queryModelUrl() {
        ReturnMsg<ModelUrl> ret = new ReturnMsg<ModelUrl>();
        ModelUrl model = new ModelUrl();
        String commUrl = ConfigUtil.getConfig(ConfigConstants.SAWP_CONFIG_PATH,
                ConfigConstants.MAIN_PUSH_COMM_URL, ConfigConstants.DEFAULT_MAIN_PUSH_COMM_URL);
        String percentUrl = ConfigUtil.getConfig(ConfigConstants.SAWP_CONFIG_PATH,
                ConfigConstants.MAIN_PUSH_PERCENT_URL,
                ConfigConstants.DEFAULT_MAIN_PUSH_PERCENT_URL);
        model.setCommModelUrl(commUrl);
        model.setPercentModelUrl(percentUrl);
        ret.setData(model);
        return setGsonView(ret);
    }
}
